//+-----------------------------------------------------------------------------
//| Included files
//+-----------------------------------------------------------------------------
#include "Register.h"


//+-----------------------------------------------------------------------------
//| Global objects
//+-----------------------------------------------------------------------------
REGISTER Register;

REGISTER::~REGISTER() {
	free(WarcraftDirectory);
}

void REGISTER::CloseKey(HKEY Key)
{
	if(Key != NULL) RegCloseKey(Key);
}
bool REGISTER::OpenKey(HKEY& Key, HKEY Parent, char *Name)
{
	if(RegOpenKeyEx(Parent, Name, 0, KEY_READ, &Key) != ERROR_SUCCESS)
	{
		//Error.SetMessage("Unable to open the registry key \"" + Name + "\"!");
		return FALSE;
	}

	return TRUE;
}
bool REGISTER::GetStringValue(HKEY Key, char *Name)
{
	DWORD Type;
	DWORD Size;

	if(RegQueryValueEx(Key, Name, 0, &Type, NULL, &Size) != ERROR_SUCCESS)
	{
		//Error.SetMessage("Unable to get a registry key string value information!");
		return FALSE;
	}

	if(Type != REG_SZ)
	{
		//Error.SetMessage("The registry key is not a string!");
		return FALSE;
	}

	if(WarcraftDirectory != NULL) free(WarcraftDirectory);
	WarcraftDirectory = (char *)malloc(Size+1);

	if(RegQueryValueEx(Key, Name, 0, NULL, (LPBYTE)WarcraftDirectory, &Size) != ERROR_SUCCESS)
	{
		//Error.SetMessage("Unable to get a registry key string value!");
		return FALSE;
	}
	WarcraftDirectory[Size] = NULL;
	return TRUE;
}
bool REGISTER::FindWarcraftDirectory()
{
	BOOL Result = FALSE;
	HKEY SoftwareKey = NULL;
	HKEY SoftwareBlizzardKey = NULL;
	HKEY SoftwareBlizzardWarcraftKey = NULL;

	while(TRUE)
	{
		if(!OpenKey(SoftwareKey, HKEY_CURRENT_USER, "Software")) break;
		if(!OpenKey(SoftwareBlizzardKey, SoftwareKey, "Blizzard Entertainment")) break;
		if(!OpenKey(SoftwareBlizzardWarcraftKey, SoftwareBlizzardKey, "Warcraft III")) break;

		if(!GetStringValue(SoftwareBlizzardWarcraftKey, "InstallPath")) break;

		Result = TRUE;
		break;
	}

	CloseKey(SoftwareBlizzardWarcraftKey);
	CloseKey(SoftwareBlizzardKey);
	CloseKey(SoftwareKey);
	return TRUE;
}